//必须带done回调 执行完成调用
import isArray from 'lodash/isArray'
import { getModelInfo } from '@ve/utils/getModelInfo'
import { ElMessage } from 'element-plus'
import { translateTitle } from '@/utils/i18n'
import unionBy from 'lodash/unionBy'

export default function getChartData({ vm, action, done }) {
  let moduleId = action.extension.moduleId.value
  let _modelInfo = getModelInfo(vm, moduleId)
  if (!moduleId) {
    ElMessage({
      showClose: true,
      message: translateTitle('Ve.eventTask.调用接口的组件'),
      type: 'error',
    })
  }
  if (!_modelInfo.extension.requestSet?.value?.api) {
    ElMessage({
      showClose: true,
      message: translateTitle('Ve.eventTask.组件未配置接口'),
      type: 'error',
    })
  }
  let _data = {}
  _data = {
    ..._modelInfo.data.formData,
    ...{ '#LIMIT#': 1 },
  }
  _modelInfo.data = _data
  if (_data && _modelInfo.extension.requestSet.value?.data) {
    let arr = []
    Object.keys(_data).map((item) => {
      arr.push({ key: item, value: _data[item] })
    })
    let reqDataList = _modelInfo.extension.requestSet.value?.data
    _modelInfo.extension.requestSet.value.data = unionBy(reqDataList, arr)
  }
  if (isArray(moduleId)) {
    moduleId.forEach((item) => {
      let _module = vm.vuiComponents[item]
      if (_module) {
        //TODO: 此处注释掉是因为在各种图表中都有watch info的改变,然后去请求接口,不需要主动调用,应该需要优化
        // _module.self && _module.self.getRequestData()
      }
    })
  }
  done(null, {})
}
